package com.axin.system.controller;

import com.axin.common.core.domain.web.AjaxResult;
import com.axin.common.security.base.BaseController;
import com.axin.common.security.model.LoginUser;
import com.axin.system.api.domain.SysUser;
import com.axin.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 个人信息 业务处理
 *
 * @Author axin
 * @Date 2021/10/26 20:03
 **/
@RestController
@RequestMapping("/user/profile")
public class SysProfileController extends BaseController {


    @Autowired
    private ISysUserService userService;


    /**
     * 个人信息
     */
    @GetMapping
    public AjaxResult profile() {
        LoginUser loginUser = getLoginUser();
        SysUser user = loginUser.getUser();
        Map<String, Object> ajax = new HashMap<>();
        ajax.put("user", user);
        ajax.put("roleGroup", userService.selectUserRoleGroup(user.getUserId()));
        return AjaxResult.success(ajax);
    }


    /**
     * 头像上传
     */
    @PostMapping("/avatar")
    public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file) throws IOException {
        if (!file.isEmpty()) {
            LoginUser loginUser = getLoginUser();
//            SysOss oss = iSysOssService.upload(file);
//            String avatar = oss.getUrl();
//            if (userService.updateUserAvatar(loginUser.getUsername(), avatar))
//            {
//                Map<String,Object> ajax = new HashMap<>();
//                ajax.put("imgUrl", avatar);
//                // 更新缓存用户头像
//                loginUser.getUser().setAvatar(avatar);
//                tokenService.setLoginUser(loginUser);
//                return AjaxResult.success(ajax);
//            }
        }
        return AjaxResult.error("上传图片异常，请联系管理员");
    }
}
